package hp.com.service.impl;

import hp.com.dao.CartDataDAO;
import hp.com.dao.impl.CartDataDAOImpl;
import hp.com.entity.CartData;
import hp.com.service.CartDataService;

import java.util.List;

public class CartDataServiceImpl implements CartDataService {
    private CartDataDAO dao = new CartDataDAOImpl();

    // 查看购物车
    @Override
    public List<CartData> BuyCarShow(String name) throws Exception {
        return dao.BuyCarShow(name);
    }
    //删除购物车数据
    @Override
    public void DeleteCartData(int id) throws Exception {
        dao.DeleteCartData(id);
    }
    //查询单个购物车数据
    @Override
    public CartData GetCartDataById(int id) throws Exception {
        return dao.GetCartDataById(id);
    }

    @Override
    public double UpdateAmount(CartData cart, int amount) throws Exception {
        double singlePrice = cart.getSinglePrice();
        double newPrice = singlePrice*amount;
        dao.UpdateAmountAndPrice(cart.getDataId(),amount,newPrice);
        return newPrice;
    }

    @Override
    public CartData getCartData(int itemId, int userId, String suk, int amount) throws Exception{
        CartData cart=new CartData();
        cart.setUserId(String.valueOf(userId));
        cart.setItemId(String.valueOf(itemId));
        cart.setItemNumber(amount);
        cart.setSuk(dao.GetTextSUKNPriceSUK(suk).get("suk"));
        cart.setPic(dao.GetFirstPicById(itemId));
        cart.setPrice((Double.valueOf(dao.GetTextSUKNPriceSUK(suk).get("price")))*amount);
        return cart;
    }

    @Override
    public boolean AddToCart(CartData cart) throws Exception {
       return dao.AddDataToCart(cart);
    }
}

